<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>猜数字</title>
</head>
<body>
<h3>已生成一个1~100之间的随机整数：</h3>
<input type="text" placeholder="请输入你猜的数字">
<button onclick="guess()">点我验证</button>
<h3>小提示： <span></span></h3>

<script>
    //定义变量用来保存猜的次数
    var count = 1;
    var r = parseInt(Math.random() * 100) + 1;
    console.log('打个小抄' + r)

    function guess() {
        let num = document.querySelector('input').value;
        //判断是否为整数
        if (isNaN(num)) {
            alert('请输入数字');
            return;
        }
        console.log(num);
        num = parseInt(num);
        //获取用来显示结果的span标签
        if (num > r) {
            document.querySelector('span').innerText = '猜大了';
            //没猜一次，次数＋1
            count++;
        } else if (num < r) {
            document.querySelector('span').innerHTML = '猜小了';
            count++;
        } else {
            document.querySelector('span').innerHTML = '猜了' + count + '次终于对了';
            alert('恭喜你猜对了');
        }
    }
</script>
</body>
</html>